#ifndef THREADWORK_H
#define THREADWORK_H
#include <QThread>
#include <QDebug>
#include <Windows.h>
#include <QSerialPort>        //提供访问串口的功能
#include "qsettings.h"
#include "IIRDesign/IIRFilterDesign.h"
#include "Config/constant.h"
#include "HRVPro/hrvdataprocess.h"
#include "Beans/userinfobeab.h"
#include "HRVPro/ppgrealprocess1.h"
#include "HRVPro/ppgrealprocess2.h"


class ThreadWork : public QThread
{
    Q_OBJECT
public:
    explicit ThreadWork(QObject *parent = nullptr);
    ~ThreadWork();
    QString CharArrayToHexString(QByteArray b,int len);
    void savetxtfile(QString filename,QList<double> l);
    void savetxtfile(QString filename,QList<int> l);
    void savetxtfile2(QString filename,QList<double> l1,QList<double> l2);
    void SetRunStop(){
        RunFlag=false;
    }



    bool getSerialState()
    {
        return SerialState;
    }
    bool InitialSerialPort()
    {
        QSettings *setting=new QSettings("./Configuration.ini",QSettings::IniFormat);
        setting->beginGroup("Config");
        QString comName=setting->value("COM").toString();
        QString mt=setting->value("Time").toString();
        mt=mt.mid(0,1);
        qDebug()<<"mt=="<<mt;
        MeasureTime=mt.toInt();
        qDebug()<<"MeasureTime=="<<MeasureTime;
        setting->endGroup();
        qDebug()<<comName;
        qserialPort=new QSerialPort;
        qserialPort->setPortName(comName);
        qserialPort->setBaudRate(460800);
        qserialPort->setDataBits(QSerialPort::Data8); // 设置数据位为8位
        qserialPort->setParity(QSerialPort::NoParity); // 设置无校验位
        qserialPort->setStopBits(QSerialPort::OneStop); // 设置停止位为1
        qserialPort->setFlowControl(QSerialPort::NoFlowControl); //设置无流控制
        delete setting;

        bool success= qserialPort->open(QIODevice::ReadWrite);
        if(success){
            QByteArray b;
            b.append(0x31);
            qserialPort->write(b);
        }
        return success;
    }


signals:
    void sendData(double ppg1,double appg1,double ppg2,double appg2);//双人模式下数据的发送
    void sendHR2UI(unsigned char HR,int p);
    void sendSPOX2UI(double spox,int p);
    void sendRR2UI(int rr,int p);
    void sendProgress2UI(int progress,int p);

    void sendOver(int p);
    void sendOver2(int p);
private:


    bool SingleOrGroup;//真-单人，假-双人
    bool RunFlag=true;
    bool SerialState=false;
    QSerialPort *qserialPort;
    int MeasureTime;
    HRVDataProcess hrvDataProcess;
    bool StartFlag1,StartFlag2;//双路开始检测标识-信号质量判断
    bool StartFlag11,StartFlag22;//双路开始检测标识-开始按钮控制
    int p1Num,p2Num;
    UserInfoBeab uib1,uib2;
    PPGRealProcess1 *ppgp1;
    PPGRealProcess2 *ppgp2;
    double a_ppg_lp[3],b_ppg_lp[3];
    double appg_a1[3];double appg_b1[3];//滤波器参数appg_a1，b1  用于差分脉搏波滤波
    double appg_a2[3];double appg_b2[3];//滤波器参数appg_a2, b2  用于二级差分脉搏波
    double lp_filter_ir1(double raw_ir);
    double lp_filter_ir2(double raw_ir);
    double getAPPG1(double raw_ppg);
    double getAPPG2(double raw_ppg);
    void initial();
    QList<int>rr1,rr2;
    QList<double>spox_list1,per_list1,spox_list2,per_list2;
    QList<double>tempPPG1;
    QList<double>tempPPG2;
    QList<double>PPGforAPPG1;
    QList<double>PPGforAPPG2;

    QList<double>IR1forSave;
    QList<double>Red1forSave;
    QList<double>IR2forSave;
    QList<double>Red2forSave;
    int SigQualityFlag1;
    int SigQualityFlag2;
    void saveData1();
    void saveData2();
    bool Sq(QList<double>ppg);
    int SampleNum;


private slots:
    void readComDataSlot();
    void getSPOX(double ox,int p);
    void getHR(unsigned char ox,int p);
    void getRR(int rr,int p);
    void getPerfusion(double perfusion,int p);
    void getUserInfo(UserInfoBeab uib,int p);
    void getStartOrStop(bool b,int p);//单人模式
    void getStartOrStop2(bool b,int p);//双人模式

protected:
    void run();// 线程入口
};

#endif // THREADWORK_H
